package pure.utils 
{
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.geom.Matrix;
	import flash.net.LocalConnection;
    import flash.system.ApplicationDomain;
    import flash.utils.*;

	
	/**
	 * 类型×反射(实用类)
	 * @author Des
	 */
	
public class DesU 
{

	 /**
	  * 获取位图资源
	  * 
	  * @param definition			类定义
	  * @param applicationDomain	应用域
	  */
	public static function getBitmapByDefinition(definition:String, applicationDomain:ApplicationDomain = null) :BitmapData
	{
		if (!applicationDomain)
		{
			applicationDomain = ApplicationDomain.currentDomain;
		}
		if (applicationDomain.hasDefinition(definition))
		{
			var assetClass:Class = applicationDomain.getDefinition(definition) as Class;
			var bitmapData:BitmapData = new assetClass(0, 0);
		} 
		else 
		{
			throw new Error("<DesU::getBitmapByDefinition()::"+definition+">类定义不存在!");
		}
		return bitmapData;	
	}
	

	
	/**
	 * 对象是否是要的类型
	 * @param target 显示对象 
	 * @param type 目标类型
	 */
	public static function checkTypeByParent(target:*,type:Class):Boolean
	{
		if(target is type)
		{
			return true;
		}
		else if(target && target.parent)
		{
			return arguments.callee(target.parent,type);
		}
		return false; 
	}

	
	/**
	 * 对象 → 类型名称
	 * 
	 * @param object
	 * @param simple
	 */
	public static function getClassName(object:Object, simple:Boolean = true):String
	{
		var string:String = getQualifiedClassName(object);
		string = string.replace("::",".");
		if(simple)
		{
			string = string.substr(string.lastIndexOf(".") + 1);
		}
		
		return string;
	}
	
	
	/**
	 * 对象 → 类型
	 * 
	 * @param item			对象
	 */
	public static function getClass(item:*):Class
	{
		if(item is Class || item == null)
		{
			return item;
		}
		
		return Object(item).constructor;
	}

	
	/**
	 * 回收
	 */
	public static function gc():void
	{
		try
		{
			new LocalConnection().connect("gc");
			new LocalConnection().connect("gc");
		}
		catch (error : Error)
		{
		}
	}

	
	
	
	
	
	
	
	/**
	 * 绝对值
	 */
	static public function abs(Value:Number):Number
	{
		return (Value>0)?Value:-Value;
	}
	
	
	/**
	 * 向下取整
	 */
	static public function floor(Value:Number):Number
	{
		var number:Number = int(Value);
		return (Value > 0)?(number):((number != Value)?(number - 1):(number));
	}
	
	
	/**
	 * 向上取整
	 */
	static public function ceil(Value:Number):Number
	{
		var number:Number = int(Value);
		return (Value > 0)?((number != Value)?(number + 1):(number)):(number);
	}
	
	
	/**
	 * 最接近整数
	 */
	static public function round(Value:Number):Number
	{
		var number:Number = int(Value + ((Value > 0)?0.5: -0.5));
		return (Value > 0)?(number):((number != Value)?(number - 1):(number));
	}
	
	
	/**
	 * 返回指定数值，位于最小与最大值之间
	 */
	public static function clamp(v:Number, min:Number = 0, max:Number = 1):Number
	{
		if(v < min)    return min;
		if(v > max)    return max;
		return v;
	}

	

	/**
	 * 解析角度生成帧
	 * @param angle
	 * @param anglePerUnit		每个转向的相差角度
	 **/
	public static function parseFrameByDegrees( angle:Number , anglePerUnit:int = 5 ):int {
		var frame:int;
		if (angle > 360 - anglePerUnit/2 || angle < anglePerUnit/2 ) {
			frame = 1;
		}
		else {
			frame = int((angle - anglePerUnit / 2) / anglePerUnit) + 2;
		}
		return frame;
	}
}
}